<?php

/**
 * Created by Sven.
 * Date: 2016/12/23
 * Time: 15:48
 *  操作cms_level表的数据库实体类
 */
class LevelModel extends Model {
    protected  $_table = 'cms_level'; // 该模型对应的表名

    private $id; // 编号
    private $level_name; // 等级名称
    private $level_info; // 等级密码

    private $limit;

    // 拦截器
    public function __get( $name ) {
        return $this->$name;
    }

    public function __set( $name, $value ) {
        // 开启输入验证
        $this->$name = Tool::mysqlString($value);
    }

    public function addLevel() {
        $_sql = "INSERT 
                                  INTO 
                                  cms_level(
                                    level_name,
                                    level_info
                                  ) 
                                  VALUES (
                                      '$this->level_name',
                                      '$this->level_info'
                                  );";

        return parent::execute( $_sql );
    }

    public function deleteLevel() {
        $_sql = "DELETE 
                                  FROM cms_level
                                  
                                  WHERE  id = '$this->id'
                                  LIMIT 1;
                        ";
        return parent::execute( $_sql );
    }

    public function updateLevel() {
        $_sql = "UPDATE 
                                    cms_level 
                                    set 
                                        level_name='$this->level_name', 
                                        level_info='$this->level_info'
                                         
                                    where
                                      id='$this->id' 
                                    
                                    LIMIT 1; ";


        return parent::execute( $_sql );
    }

    public function getSingleLevel() {
        $_sql = "SELECT 
                                id, 
                                level_name, 
                                level_info 
                            FROM 
                              cms_level
                            WHERE
                              id = '$this->id'
                              OR 
                              level_name = '$this->level_name';
                             ";

        return parent::one( $_sql );
    }

    public function getLevelCountByName() {
        $_sql = "SELECT 
                                    COUNT(1)  as count
                                    
                                     FROM 
                                                   cms_level
                                      WHERE  level_name = '$this->level_name'  ";

        return parent::one( $_sql )->count;
    }


    public function getAllLevels() {
        $_sql = "SELECT 
                                id, 
                                level_name,
                                level_info
                            FROM 
                              cms_level
                            WHERE
                              1=1
                            ORDER BY 
                              id DESC
                             $this->limit; 
                             ";

        return parent::query($_sql);
    }

    public function  getTotalCount(){
        return parent::getTotalCount($this->_table);
    }

}